package com.woniu.user.service.impl;

import com.woniu.user.model.Notice;
import com.woniu.user.dao.NoticeDao;
import com.woniu.user.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements NoticeService {

    @Resource
    private JavaMailSender javaMailSender;

    // 向学员发送信息
    @SneakyThrows
    @Override
    public void sendMail(String email, String title, String content) {
        MimeMessage message = javaMailSender.createMimeMessage();
        message.setFrom(new InternetAddress("xuxilin1997@163.com"));//设置发送者
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));//设置接收者
        message.setContent(content, "text/html;charset=utf-8");//设置邮件内容
        message.setSubject(title);
        javaMailSender.send(message);
        log.info("发送了邮件{} {} {}", email, title, content);
    }
}
